/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.eventbus;

import com.google.common.eventbus.EventBus;

/**
 * EventBus的默认配置下，只能通过方法的参数来区分侦听器。
 * 但是参数并没有特殊标志，后期的代码维护会变得十分困难，很难通过广播查找侦听器。
 * 为了解决这个问题，增加一个事件接口，可以通过事件的类型来找到侦听器。
 * 所有的事件都必须继承自这个接口。
 * <p>
 * IntelliJ reports unstable API usages.
 * Note that the presence of this warning implies nothing about the quality or
 * performance of the API in question, only the fact that it is not "API-frozen."
 * <p>
 * It is generally safe for <i>applications</i> to depend on beta APIs, at
 * the cost of some extra work during upgrades.
 * <p>
 * noinspection
 *
 * @author Mr.css
 * @version 2019-12-07 11:44
 */
//@SuppressWarnings(Warning.UNSTABLE_API_USAGE)
public class Eventbus {
    private Eventbus() {
    }

    private static EventBus eventBus = new EventBus();

    /**
     * 可以设置新的数据总线
     *
     * @param eventBus Eventbus
     */
    public static void setEventBus(EventBus eventBus) {
        Eventbus.eventBus = eventBus;
    }

    /**
     * invoke {@link EventBus#post}, all EventBus-event must implement Event-interface.
     *
     * @param event Event
     */
    public static void post(Event event) {
        eventBus.post(event);
    }

    /**
     * invoke {@link EventBus#unregister}
     *
     * @param object object whose subscriber methods should be unregistered.
     */
    public static void unregister(Object object) {
        eventBus.unregister(object);
    }

    /**
     * invoke {@link EventBus#register}
     *
     * @param object object whose subscriber methods should be registered.
     */
    public static void register(Object object) {
        eventBus.register(object);
    }
}
